import { useModel } from "umi";
import { useReactive } from "ahooks";
import compress from "gql-compress";
import { useEffect } from "react";

const useAllPersonVersions = () => {
    const { graphqlFetchPlatform } = useModel("global");

    const state = useReactive({
        loading: false,
        versions: [],
    });

    const loadVersions = async () => {
        state.loading = true;
        let res = await graphqlFetchPlatform({
            query,
            variables: {},
        });
        state.versions = res?.a?.nodes || [];
        state.loading = false;
    };

    useEffect(() => {
        loadVersions();
    }, []);

    return { ...state, loadVersions };
};

const query = compress(`query MyQuery {
    a: allPersonReferenceDatabaseVersions( orderBy: CREATE_TIME_DESC ) {
        totalCount
        nodes {
            compareVersion
            databaseName
            structureObjects
            patchData
            versionNo
            uuid
            createTime
            creater
            publishRecord
        }
    }
}`);

export default useAllPersonVersions;
